package com.gollum.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import com.gollum.common.utils.TreeUtils;
import com.gollum.jpa.common.Specifications;
import com.gollum.jpa.service.JpaServerImpl;
import com.gollum.system.domain.model.SysDictType;
import com.gollum.system.service.SysDictTypeService;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.gollum.common.constant.NumberConstants.ZERO_STR;

/**
 * 字典实现
 *
 * @author Herther
 * @createTime 2022年09月17日 22:48:00
 * @since 1.0.0
 */
@Service
public class SysDictTypeServiceImpl extends JpaServerImpl<SysDictType,Long> implements SysDictTypeService {


    private Specification<SysDictType> getParams(){
        return Specifications.<SysDictType>and()
                .eq(SysDictType::getStatus, 0)
                .build();

    }


    /**
     *
     * 构建前端搜索树 字典类型
     *
     * @param sysDictTypes
     * @return List<Tree<String>>
     * @author Herther
     * @since 1.0.0
     * @date: 2022/09/19 0:01
     */
    private List<Tree<String>> buildTree(List<SysDictType> sysDictTypes){
        //转换器
        List<Tree<String>> treeNodes = TreeUtils.build(sysDictTypes, ZERO_STR, TreeUtils.getTreeComponentNodeConfig(),
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    tree.setWeight(treeNode.getSort());
                    tree.setName(treeNode.getDictName());
                    // 父节点不让选中 --配合前端
                    tree.putExtra(TreeUtils.TreeConstants.SELECTABLE, treeNode.getParentId() == 0 ? false : true);
                });
        return treeNodes;
    }

    @Override
    public List<Tree<String>> list() {
        return buildTree(this.findAll(getParams(), getDescSort()));
    }
}
